package cn.edu.scau.cmi.zenglinghong.client;

import cn.edu.scau.cmi.zenglinghong.Singleton.Chairman;
import cn.edu.scau.cmi.zenglinghong.Singleton.Marshal;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SingletonClient extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	public static void init(Stage primaryStage) {
		try {
			Label label1 = new Label("主席名称:");
			TextField singleT = new TextField();
			Button button1 = new Button("创建单例模式对象");
			StringBuilder singleOut = new StringBuilder();
			TextArea singleShow = new TextArea();
			singleShow.setEditable(false);
			
			Label label2 = new Label("元帅名称:");
			TextField mulT = new TextField();
			Button button2 = new Button("创建多例模式对象");
			StringBuilder mulOut = new StringBuilder();
			TextArea mulShow = new TextArea();
			mulShow.setEditable(false);

			VBox root = new VBox();
			root.setSpacing(10);
			Scene scene = new Scene(root,600,600);
			primaryStage.setScene(scene);
			
			button1.setOnAction(sigHandle -> {
				Chairman chairman = Chairman.getInstance(singleT.getText());
				singleOut.append("名称" + chairman.getName() + ", 对象:" + chairman + "\n");
				singleShow.setText(singleOut.toString());
			});
			singleShow.setMaxSize(600, 200);
			singleShow.setMinSize(600, 200);
			root.getChildren().add(label1);
			root.getChildren().add(singleT);
			root.getChildren().add(button1);
			root.getChildren().add(singleShow);
			
			button2.setOnAction(sigHandle -> {
				if (Marshal.getSize()<Marshal.maxNumber) {
					Marshal malshal = Marshal.getInstance(mulT.getText());
					mulOut.append("名称" + malshal.getName() + ", 对象:" + malshal + "\n");					
				} else {
					mulOut.append("已创建"+Marshal.maxNumber+"个对象\n");
				}
				mulShow.setText(mulOut.toString());
			});
			mulShow.setMaxSize(600, 200);
			mulShow.setMinSize(600, 200);
			root.getChildren().add(label2);
			root.getChildren().add(mulT);
			root.getChildren().add(button2);
			root.getChildren().add(mulShow);
			
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		init(primaryStage);
	}

}
